package o;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.text.TextUtils;
import androidx.annotation.DrawableRes;
import com.huawei.hms.framework.common.ContainerUtils;
import com.tencent.mm.opensdk.modelmsg.WXMediaMessage;
import java.io.ByteArrayOutputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.regex.Pattern;
import o.cwj.i;

public abstract class cxw {
    public static String ॱ(String str) {
        StringBuilder stringBuilder = new StringBuilder();
        int length = str.length();
        for (int i = 0; i < length; i++) {
            String valueOf = String.valueOf(str.charAt(i));
            if (!(Character.isDigit(str.charAt(i)) || ˎ(valueOf) || ":".equals(valueOf) || "/".equals(valueOf) || "?".equals(valueOf) || ContainerUtils.KEY_VALUE_DELIMITER.equals(valueOf) || ContainerUtils.FIELD_DELIMITER.equals(valueOf) || "%".equals(valueOf))) {
                try {
                    valueOf = URLEncoder.encode(URLEncoder.encode(valueOf, "UTF-8"), "UTF-8");
                } catch (UnsupportedEncodingException e) {
                    cwr.ॱ.ˏ("ShareUtils", "weibo share url encode error");
                }
            }
            stringBuilder.append(valueOf);
        }
        return stringBuilder.toString();
    }

    private static boolean ˎ(String str) {
        return Pattern.compile("[a-zA-Z]+").matcher(str).matches();
    }

    public static byte[] ˎ(Context context, Bitmap bitmap, @DrawableRes int i, int i2, boolean z) {
        byte[] bArr = null;
        if (bitmap != null) {
            try {
                bArr = ॱ(bitmap, i2, z);
            } catch (IllegalStateException e) {
                cwr.ॱ.ˊ("ShareUtils", "bmpToByteArray error: " + e.toString());
            }
        }
        if (bArr != null && bArr.length != 0) {
            return bArr;
        }
        Bitmap ॱ = ॱ(context, i);
        if (ॱ != null) {
            return ॱ(ॱ, i2, true);
        }
        return bArr;
    }

    public static Bitmap ॱ(Context context, int i) {
        if (context == null) {
            return null;
        }
        if (i == 0) {
            i = cwj$b.ˊ;
        }
        return BitmapFactory.decodeResource(context.getResources(), i);
    }

    private static byte[] ॱ(Bitmap bitmap, int i, boolean z) {
        int i2 = 100;
        OutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        cwr.ॱ.ˋ("ShareUtils", "IconBitmap compress!");
        if (z) {
            bitmap.compress(CompressFormat.PNG, 100, byteArrayOutputStream);
        } else {
            bitmap.compress(CompressFormat.JPEG, 100, byteArrayOutputStream);
        }
        Matrix matrix = new Matrix();
        while (byteArrayOutputStream.toByteArray().length > i * WXMediaMessage.DESCRIPTION_LENGTH_LIMIT) {
            byteArrayOutputStream.reset();
            i2 -= 2;
            if (i2 <= 0) {
                break;
            } else if (z) {
                ˊ(bitmap, i2, byteArrayOutputStream, matrix);
            } else {
                bitmap.compress(CompressFormat.JPEG, i2, byteArrayOutputStream);
            }
        }
        byte[] toByteArray = byteArrayOutputStream.toByteArray();
        try {
            byteArrayOutputStream.close();
        } catch (Throwable e) {
            cwr.ॱ.ˊ("ShareUtils", "weixin share bitmap output close error:", e);
        }
        return toByteArray;
    }

    private static void ˊ(Bitmap bitmap, int i, ByteArrayOutputStream byteArrayOutputStream, Matrix matrix) {
        float f = ((float) i) / 100.0f;
        matrix.setScale(f, f);
        Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true).compress(CompressFormat.PNG, 100, byteArrayOutputStream);
    }

    public static boolean ˏ(String str) {
        if (TextUtils.isEmpty(str) || (!str.contains(".png") && !str.contains(".PNG"))) {
            return false;
        }
        return true;
    }

    public static String ॱ(Context context, String str, String str2) {
        if (TextUtils.isEmpty(str)) {
            cwr.ॱ.ˋ("ShareUtils", "can not find any url.");
            return "";
        }
        String ॱ = ॱ(str);
        StringBuilder stringBuilder = new StringBuilder(ॱ);
        if (ॱ.contains("?")) {
            stringBuilder.append(ContainerUtils.FIELD_DELIMITER);
        } else {
            stringBuilder.append("?");
        }
        stringBuilder.append("shareTo");
        stringBuilder.append(ContainerUtils.KEY_VALUE_DELIMITER);
        stringBuilder.append(str2);
        stringBuilder.append(ContainerUtils.FIELD_DELIMITER);
        stringBuilder.append("shareFrom");
        stringBuilder.append(ContainerUtils.KEY_VALUE_DELIMITER);
        stringBuilder.append(context.getString(i.ˋॱ));
        return stringBuilder.toString();
    }

    public static byte[] ˏ(Bitmap bitmap, int i, int i2, boolean z) {
        ByteArrayOutputStream byteArrayOutputStream;
        Object e;
        Throwable th;
        byte[] bArr = null;
        if (z) {
            try {
                byteArrayOutputStream = null;
                bArr = ॱ(bitmap, i, true);
            } catch (Exception e2) {
                e = e2;
                byteArrayOutputStream = null;
                try {
                    cwr.ॱ.ˊ("ShareUtils", "put thumb failed:" + e);
                    if (byteArrayOutputStream != null) {
                        try {
                            byteArrayOutputStream.close();
                        } catch (Throwable e3) {
                            cwr.ॱ.ˊ("ShareUtils", "weibo share bitmap output close error:", e3);
                        }
                    }
                    return bArr;
                } catch (Throwable th2) {
                    th = th2;
                    if (byteArrayOutputStream != null) {
                        try {
                            byteArrayOutputStream.close();
                        } catch (Throwable e32) {
                            cwr.ॱ.ˊ("ShareUtils", "weibo share bitmap output close error:", e32);
                        }
                    }
                    throw th;
                }
            } catch (Throwable e322) {
                byteArrayOutputStream = null;
                th = e322;
                if (byteArrayOutputStream != null) {
                    byteArrayOutputStream.close();
                }
                throw th;
            }
        }
        byteArrayOutputStream = new ByteArrayOutputStream();
        try {
            bitmap.compress(CompressFormat.JPEG, i2, byteArrayOutputStream);
            int length = ((i * WXMediaMessage.DESCRIPTION_LENGTH_LIMIT) * i2) / byteArrayOutputStream.toByteArray().length;
            if (length < 100 && length > 0) {
                byteArrayOutputStream.reset();
                bitmap.compress(CompressFormat.JPEG, length, byteArrayOutputStream);
            }
            bArr = byteArrayOutputStream.toByteArray();
        } catch (Exception e4) {
            e = e4;
            cwr.ॱ.ˊ("ShareUtils", "put thumb failed:" + e);
            if (byteArrayOutputStream != null) {
                byteArrayOutputStream.close();
            }
            return bArr;
        }
        if (byteArrayOutputStream != null) {
            try {
                byteArrayOutputStream.close();
            } catch (Throwable e3222) {
                cwr.ॱ.ˊ("ShareUtils", "weibo share bitmap output close error:", e3222);
            }
        }
        return bArr;
    }

    private static Bitmap ॱ(Bitmap bitmap, int i) {
        Matrix matrix = new Matrix();
        matrix.reset();
        matrix.setRotate((float) i);
        return Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
    }

    public static Bitmap ˊ(Bitmap bitmap, boolean z) {
        Bitmap ॱ;
        long j;
        long j2;
        long j3;
        if (z) {
            ॱ = ॱ(bitmap, 270);
        } else {
            ॱ = bitmap;
        }
        long width = (long) ॱ.getWidth();
        long height = (long) ॱ.getHeight();
        if (z) {
            j = (4 * height) / 3;
            j2 = (width - j) / 2;
            j3 = 0;
        } else {
            j2 = 0;
            j3 = (11 * height) / 100;
            height = (3 * width) / 4;
            j = width;
        }
        try {
            ॱ = Bitmap.createBitmap(ॱ, (int) j2, (int) j3, (int) j, (int) height, null, false);
        } catch (IllegalArgumentException e) {
            cwr.ॱ.ˊ("ShareUtils", "create bitmap error!");
        }
        return ॱ;
    }
}
